using System;
using System.Data.SqlClient;

namespace ILMXFrameWork.XMA.SQL
{
	/// <summary>
	/// Create a connection to SQL 2000/2005
	/// </summary>
    /*Copyright (c) 2009, Joseph Stepongzi
     All rights reserved.
     Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
     * Neither the name of Joseph Stepongzi nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
     AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
     STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
      */
    public class SQLConnection
    {
        // variables
        private string _connectionString = null;
        private System.Data.SqlClient.SqlConnection _sqlConnection = null;
        private System.Data.SqlClient.SqlCommand _sqlCommand = null;
        private System.Data.SqlClient.SqlDataAdapter _sqlDa = null;
        private string _sqlQuery = null;
        private string _server = null;
        private string _db = null;
        private string _user = null;
        private string _pswd = null;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="server">The name of the sql server to connect to.</param>
        /// <param name="db">The database instance to connect to.</param>
        /// <param name="user">The username used to connect to sql.</param>
        /// <param name="pswd">The password used to connect to sql.</param>
        public SQLConnection(string server, string db, string user, string pswd)
        {
            this._server = server;
            this._db = db;
            this._user = user;
            this._pswd = pswd;
            this._connectionString = "Data Source=" + _server + ";Initial Catalog=" + _db + ";User Id=" + _user + ";Password=" + _pswd + ";trusted_Connection=False";
            this._sqlConnection = new SqlConnection(_connectionString);
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="server">The name of the sql server to connect to.</param>
        /// <param name="db">The database instance to connect to.</param>
        public SQLConnection(string server, string db)
        {
            //
            // TODO: Add code for not putting in a password
            //
            this._server = server;
            this._db = db;
            this._connectionString = "Data Source=" + _server + ";Initial Catalog=" + _db + ";Integrated Security=SSPI";
            this._sqlConnection = new SqlConnection(_connectionString);
        }

        /// <summary>
        /// Write a SQL Query to the SQL Connection.
        /// </summary>
        /// <param name="sqlQuery">The write query to send to the SQL Connection.</param>
        public void WriteQuery(string sqlQuery)
        {
            this._sqlQuery = sqlQuery;
            this._sqlCommand = new SqlCommand(_sqlQuery, _sqlConnection);
            this._sqlCommand.ExecuteNonQuery();
        }

        /// <summary>
        /// Get the SQL Data Adapter back from the Sql Connection.
        /// </summary>
        /// <param name="sqlQuery">The query to send to the Sql Connection.</param>
        public System.Data.SqlClient.SqlDataAdapter ReadQueryDataAdapter(string sqlQuery)
        {
            this._sqlQuery = sqlQuery;
            this._sqlDa = new SqlDataAdapter(_sqlQuery, _sqlConnection);
            return _sqlDa;
        }

        /// <summary>
        /// Get the SQLCommand back from the Sql Connection.
        /// </summary>
        /// <param name="sqlQuery">The query to send to the Sql Connection.</param>
        public System.Data.SqlClient.SqlCommand ReadQuerySqlCommand(string sqlQuery)
        {
            this._sqlQuery = sqlQuery;
            this._sqlCommand = new SqlCommand(_sqlQuery, _sqlConnection);
            return _sqlCommand;
        }

        /// <summary>
        /// Open the Sql Connection.
        /// </summary>
        public void Open()
        {
            _sqlConnection.Open();
        }

        /// <summary>
        /// Close the Sql Connection.
        /// </summary>
        public void Close()
        {
            if (!(_sqlCommand == null))
            {
                _sqlCommand.Dispose();
                _sqlCommand = null;
            }
            if (!(_sqlDa == null))
            {
                _sqlDa.Dispose();
                _sqlDa = null;
            }
            if (!(_sqlConnection == null))
            {
                if (_sqlConnection.State == System.Data.ConnectionState.Open)
                {
                    _sqlConnection.Close();
                }
                _sqlConnection = null;
            }
        }
    }
}
